package ZZ_backup;
// OracleEmployeeDAO implementation of the 
// EmployeeDAO interface. This class can contain all
// Oracle specific code and SQL statements. 
// The client is thus shielded from knowing 
// these implementation details.


import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import LawsonTransformJava.xref.XrefDedCode;
import LawsonTransformJava.xref.XrefProcessLevel;

import LawsonTransformJava.dao.DAOFactoryOracleDB;

public class PR560RecType2_backup extends DAObject implements 
    DAOPayDeductn, Runnable {

	private XrefProcessLevel xProcessLevels = null;
	private XrefDedCode xDedCodes = null;
	private int payrollYear = 0;
	
    public PR560RecType2_backup (File output, int payrollYear, boolean allowBlanks) throws SQLException {
    // initialization 
		try {
			//Append data to FileWriter = true
			this.output = new FileWriter(this.outputFile = output, true);
			this.payrollYear = payrollYear;
			init(allowBlanks);
		} catch (IOException e) {
			// Could no write properties file
			System.out.println("IOException: " + e);
		}
  }

    public PR560RecType2_backup (FileWriter outputFile, int payrollYear, boolean allowBlanks) throws SQLException {
    // initialization 
		try {
			this.output = outputFile;
			this.payrollYear = payrollYear;
			init(allowBlanks);
		} catch (IOException e) {
			// Could no write properties file
			System.out.println("IOException: " + e);
		}
  }

  private void init(boolean allowBlanks) throws IOException, SQLException {
	    // Set the data variables
	    data = new DAOFactoryOracleDB();
	    xProcessLevels = new XrefProcessLevel();
	    xDedCodes = new XrefDedCode(allowBlanks);
	    
//	    sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME;
	    stmtData = data.getDataStatement();	    
	    countStmtData = data.getCountStatement();
	    //Set the Date Format for output
	    df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
  }
  
  public TblPayDeductn findPayDeductn() {
	  	TblPayDeductn pyd = new TblPayDeductn();
		try {
			if (whereClause==null || whereClause.equals("")) {
				whereClause = " WHERE PAYROLL_YEAR=" + payrollYear;
			} else {
				whereClause = whereClause.trim() + " AND PAYROLL_YEAR=" + payrollYear;
			}
			sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + " " + (whereClause==null?"":whereClause);
			rslt = data.getDataStatement().executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			if (rslt.next()){
				hasMoreRecs = true;
				pyd = (TblPayDeductn) createObject(rslt, pyd);
			}
			else
				hasMoreRecs = false;
			return pyd;
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
	  return null;
}

  public TblPayDeductn findPayDeductn(String employeeNumber) {
	try {
		TblPayDeductn pyd = new TblPayDeductn();
		
		if (whereClause==null) {
			whereClause = " WHERE EMPLOYEE=" + employeeNumber + " AND PAYROLL_YEAR=" + payrollYear;
		} else {
			whereClause = whereClause.trim() + " AND EMPLOYEE=" + employeeNumber + " AND PAYROLL_YEAR=" + payrollYear;
		}
		sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + " " + (whereClause==null?"":whereClause);
		rslt = data.getDataStatement().executeQuery(sqlData);
		rsmd = rslt.getMetaData();
		rslt.next();
		pyd = (TblPayDeductn) createObject(rslt, pyd);
		return pyd;
	} catch (SQLException e) {
	    // Could not connect to the database
		System.out.println("SQLException: " + e);
	} catch (InvocationTargetException ite) {
		System.out.println("InvocationException: " + ite);
	} catch (IllegalAccessException iae) {
		System.out.println("IllegalAccessException: " + iae);
	}
	return new TblPayDeductn();
  }

  public TblPayDeductn nextPayDeductn() {
	  TblPayDeductn pyd = new TblPayDeductn();
		try {
			if (rslt.next()){
				hasMoreRecs = true;
				pyd = (TblPayDeductn) createObject(rslt, pyd);
			}
			else
				hasMoreRecs = false;
			return pyd;
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
	  return null;
}

	public boolean hasMore () {
	  return hasMoreRecs;
	}

  public int countPayDeductn() {
	try {
		countRecs = data.getCountStatement().executeQuery("SELECT COUNT(*) FROM " + data.getSchema() + "." + TABLE_NAME + (whereClause==null?"":whereClause));
		countRecs.next();
		return recCount=countRecs.getInt(1);
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		}
		return 0;
  }
  
  public void run() {
	  TblPayDeductn pyd = null;
	  
	  try {
		//Update current date/time
//		Date now = new Date();
//		System.out.println(outputFile.getName() + " Start " + df.format(now));
		//Write timestamp to output file
//		output.write(outputFile.getName() + " Start " + df.format(now) + "\n");
		
		//@ TODO DEBUG BEGIN - remove lines
//		this.setWhereClause("WHERE EMPLOYEE=100466");
		//@ TODO DEBUG END   - remove lines
		pyd = findPayDeductn();
		while (hasMore()) {
			output.append(writePR560Layout(pyd) + "\n");
			output.flush();
			pyd = nextPayDeductn();
		}
		//Update current date/time
//		now = new Date();
//		System.out.println(outputFile.getName() + " Finish " + df.format(now));
		//Write timestamp to output file
//		output.write(outputFile.getName() + " Finish " + df.format(now) + "\n");
		output.close();
	} catch (IOException e) {
		// Could not write properties file
		// Could not create output file
		System.out.println("IOException: " + e);
	}

  }
  
	//Output the PR560 format - Record Type 2
	public String writePR560Layout(TblPayDeductn pyd) {
		return (
				"2" + "," +						//RecType
				"1" + "," +
				pyd.getEmployee()	       + "," +
				xProcessLevels.xlate(pyd.getProcess_level()) + "," +
//				pyd.getProcess_level() + "," +
				"," +
				pyd.getCheck_id() + "," +
				"," +
//				pyd.getDed_code() + "," +
				xDedCodes.xlate(pyd.getDed_code()) + "," +
				pyd.getCheck_date() + "," +		//Tr_date
				pyd.getCheck_date() + "," +		//Check_date
				"," +
				"," +
				"," +
				pyd.getWage_amount() + "," +
				"," +
				pyd.getDed_amt() + "," +
				pyd.getTax_wages() + "," +
				pyd.getExcess() + "," +
				"," +
				pyd.getWork_state() + "," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				pyd.getBus_nbr_grp() + "," +
				"," +
				"," +
				"," 
				);
	}
}